package com.mflex.mwms.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.annotation.Nullable;

import com.mflex.mwms.R;
import com.mflex.mwms.adapter.StockTaskAdapter;
import com.mflex.mwms.model.CustomApiResult;
import com.mflex.mwms.utils.MMKVUtils;
import com.mflex.mwms.utils.MessageUtils;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.CallBackProxy;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class StockTaskListActivity extends BaseActivity  implements AdapterView.OnItemClickListener{

    @BindView(R.id.lvTaskList)
    ListView lvTaskList;

//    @BindView(R.id.button_query)
//    SuperButton buttonQuery;
//
//    @BindView(R.id.button_clear)
//    SuperButton saveClear;

    MiniLoadingDialog mMiniLoadingDialog;

    private List<String> taskList;

    private String userName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stock_task_list);

        ButterKnife.bind(this);
        mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(this, "加载中...");

        Intent intent = getIntent();
        userName = intent.getStringExtra("userName");
        lvTaskList.setOnItemClickListener(this);

        initData();
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        return false;
    }

    private void initData() {
        mMiniLoadingDialog.show();
        XHttp.get("api/Stock/GetStockTaskList")
                .params("Warehouse", MMKVUtils.getString("Warehose", ""))
                .syncRequest(false)
                .onMainThread(true)
                .accessToken(true)
                .timeOut(120000)
                .execute(new CallBackProxy<CustomApiResult<List<String>>, List<String>>(new SimpleCallBack<List<String>>() {

                    @Override
                    public void onSuccess(List<String> response) {
                        mMiniLoadingDialog.dismiss();
                        taskList = response;
                        StockTaskAdapter stockTaskAdapter = new StockTaskAdapter(StockTaskListActivity.this, taskList);
                        lvTaskList.setAdapter(stockTaskAdapter);
                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();
//                        if (e.getCode() == 100) {
//                            new MaterialDialog.Builder(StockTaskListActivity.this)
//                                    .iconRes(R.drawable.icon_warning)
//                                    .title(R.string.tip_warning)
//                                    .content(e.getMessage())
//                                    .positiveText(R.string.lab_submit)
//                                    .show();
//                        }
//                        if (e.getCode() == 200) {
//                            new MaterialDialog.Builder(StockTaskListActivity.this)
//                                    .iconRes(R.drawable.icon_tip)
//                                    .title(R.string.tip_infos)
//                                    .content(e.getMessage())
//                                    .positiveText(R.string.lab_submit)
//                                    .show();
//                        }
                        MessageUtils.errorMsgAndVoice(StockTaskListActivity.this, e);
                    }
                }) {
                });
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent = new Intent(StockTaskListActivity.this, StockTaskListDetailActivity.class);
        intent.putExtra("taskNo", taskList.get(i));
        startActivityForResult(intent, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        initData();
    }
}